<?php
/*
 * Walker for the Front End
 */
class CrossTheme_Walker_NavMenu_Custom extends Walker_Nav_Menu{
	
	public $parent_mega_enable;
	public $parent_fullwidth;
	public $lv0_subcount;
	public $lv1_subcount;
	public $lv1_order;
	public $lv2_order;
	public $has_sidebar;
	/*
	public $lv0_desc;
	public $lv1_desc;
	public $parent_widecolumn;
	public $parent_sidebar;
	public $parent_sidebar_str;
	public $parent_static_html;
	public $parent_static_html_str;
	public $menu_config;
	
	public $sum_column_count;
	*/
	
	function __construct() {
		//parent dunt have contruct method
		//parent::__construct();
		
    }	

	/**
	 * @see Walker::start_lvl()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param int $depth Depth of page. Used for padding.
	 */
	function start_lvl( &$output, $depth = 0, $args = array() ) {
		$indent = str_repeat("\t", $depth);
		$output .= "\n$indent<ul class=\"sub-menu\">\n";
	}

	/**
	 * @see Walker::end_lvl()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param int $depth Depth of page. Used for padding.
	 */
	function end_lvl( &$output, $depth = 0, $args = array() ) {
		$indent = str_repeat("\t", $depth);
		$output .= "$indent</ul>\n";
		
		if($this->parent_mega_enable && $this->has_sidebar){
			$output .= '</div>'; //end holder
			$output .= '</div>'; //end wrapper
		}
	}
	
	/**
	 * @see Walker::start_el()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param object $item Menu item data object.
	 * @param int $depth Depth of menu item. Used for padding.
	 * @param int $current_page Menu item ID.
	 * @param object $args
	 */
	function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
		global $wp_query;
		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
		
		$megamenu_status = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_megamenu_status', true );
		$megamenu_link = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_megamenu_link', true );
		$megamenu_des = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_megamenu_des', true );
		$scroll = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_scroll', true );
		$icon = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_icon', true );
		$background_id = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_background', true );
		$background_repeat = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_background_repeat', true );
		$background_position = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_background_position', true );
		$menu_column = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_wide_column', true );
		$static_html = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_static_html', true );
		$sidebars = get_post_meta( $item->ID, CRT_MEGA_MENU.'menu_item_sidebars', true );
				
		$class_names =  '';
		$crt_after = '';
		$this->has_sidebar = false;
		/*
		echo '<pre>';
		print_r($item);
		echo '</pre>';
		*/
		
		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;
		$classes[] = 'menu-item-level' . $depth;
		if( $item->menu_order == 1 ){
			$classes[] = 'first';
		}
		
		/*
		if( (int) $item->sub_count > 0 ){
			$classes[] = "have-child";
		}
		*/
		if($depth < 3){
			if($depth == 0 ){	
				$this->parent_fullwidth = false;
				if($megamenu_status == "enabled"){
					$this->parent_mega_enable = true;
					$classes[] = 'crt-megamenu';
//					$classes[] = $menu_column == 0 ? "columns-fullwidth" : "columns-{$menu_column}";
					if($menu_column == 0){
						$this->parent_fullwidth = true;
					}
					if(strlen($sidebars) > 0){
						$classes[] = "crt-menu-sidebar";
					}
				} else {
					$this->parent_mega_enable = false;
					$classes[] = "crt-dropdown-menu";
				}
				
				$this->lv0_subcount = (int) $item->sub_count;
				
			}
			
			if( strlen($item->description) > 0 && $megamenu_des != 'enabled'){
				$append = "{$indent}\t\t<span class=\"menu-desc-lv{$depth}\">". esc_attr($item->description) ."</span>";
			}
			
			/*
			if( (int) $item->sub_count > 0 || ( $this->parent_mega_enable == true && strlen($side_bar) > 0 ) ){
				$click_icon_html = "{$indent}\t\t<span class=\"menu-drop-icon drop-icon-lv{$depth}\"></span>";
			}
			*/			
			
			if($depth == 1 && $this->parent_mega_enable){
				if( $this->parent_fullwidth == true && $menu_column > 0){
					
	//				$classes[] = "sub-column{$menu_column}";
				}

				
				if(strlen($sidebars) > 0){
					$classes[] = "crt-menu-sidebar";
				}
				
				if( !$this->parent_mega_enable ){
					$classes[] = "wd-dropdown-menu";
				}
			}
			
			if($depth == 2 && $this->parent_mega_enable){
				$this->lv2_order = $this->lv2_order + 1; 
			}			

			$classes[] = $menu_column == 0 ? "columns-fullwidth" : "columns-{$menu_column}";		
		}
		
		if(strlen($sidebars) > 0 && ($depth == 0 || $depth == 1) ){
			$this->has_sidebar = true;
			$sidebar_str = '';
			$sidebar_str .= "{$indent}<div class=\"crt-megamenu-wrapper row lvl-" . ($depth). "\">\n" ;
			$sidebar_str .= "{$indent}<div class=\"crt-megamenu-holder\">\n" ;
			if( is_active_sidebar( $sidebars ) ) {
				$sidebar_str .= '<div class="crt-megamenu-widgets sidebar-' . ($depth). '\">';
				ob_start();
					dynamic_sidebar( $sidebars );

				$sidebar_str .= ob_get_clean() . '</div>';
			}
			
			$crt_after	.= 	$sidebar_str;	 
		}
		
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

		$output .= $indent . '<li' . $id . $value . $class_names .'>';

		$atts = array();
		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
		$atts['target'] = ! empty( $item->target )     ? $item->target     : '';
		$atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
		$atts['href']   = ! empty( $item->url )        ? $item->url        : '';

		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

		$attributes = '';
		foreach ( $atts as $attr => $value ) {
			if ( ! empty( $value ) ) {
				$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
				$attributes .= ' ' . $attr . '="' . $value . '"';
			}
		}		
		
		$item_output = $args->before;
		
		$item_output .= "\n{$indent}\t<a". $attributes .'>';
		
		if( !isset($item->title) || strlen($item->title) <= 0 ){
			$item->title = $item->post_title;
		}

		
		$title = "\n{$indent}\t\t<span {$inline_style}  class='menu-label-level-{$depth}'>" . apply_filters( 'the_title', $item->title, $item->ID ) . "</span>\n";
		//$item_output.= $thumb_html ;

		$item_output.= $args->link_before . $title . $append . $args->link_after ;

		$item_output .= "{$indent}\t</a>";
		
		$item_output .= $crt_after;
		$item_output .= $args->after;

		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

	}	
	
	function end_el( &$output, $item, $depth = 0, $args = array() ) {		
		$output .= "\n{$indent}</li>\n";
	}
	
}
?>
